import router from '@/router';
import request from './request';
//如果只传递 url 参数，则使用第2个函数的逻辑；如果同时传递 url 和 params 参数，则使用第1个函数的逻辑。
export function jump(url, params = {}) {
    const fullPath = `${url}${Object.keys(params).length > 0 ? '?' + new URLSearchParams(params).toString() : ''}`;
    console.log(fullPath);
    if (router.currentRoute.fullPath == fullPath) {
        return;
    }

    if (params != null) {
        router.push({
            path: url,
            query: params
        });
    }
}
export function jumpNeedToken(url, params = {}) {
    // 从本地存储中获取存储的信息
    const token = localStorage.getItem('token');
    if (token) {
        const headers = {
            'Authorization': `Bearer ${token}`
        };
        request.post("http://localhost:9090/account/refresh", {
        }, {
            headers: headers
        })
            .then(res => {
                router.push("/")
                // 清除本地存储中的 token
                localStorage.removeItem('token');
                localStorage.setItem('token', res.data.token);
                // localStorage.setItem('userId', res.userId);
                localStorage.setItem('userInfo', res.data.userInfo);
                if (res.data.code == 401) {
                    jump('/login');
                    console.log("token不通过");
                    return;
                }
            });
    } else {
        jump('/login');
        console.log("token不通过");
        return;
    }
    console.log("token通过");
    const fullPath = `${url}${Object.keys(params).length > 0 ? '?' + new URLSearchParams(params).toString() : ''}`;
    console.log(fullPath);
    if (router.currentRoute.fullPath == fullPath) {
        return;
    }

    if (params != null) {
        router.push({
            path: url,
            query: params
        });
    }
}
export function newPage(url, params = {}) {
    if (Object.keys(params).length === 0) {
        // 如果没有 params 参数，则直接在新窗口打开页面
        window.open(url, '_blank');
    } else {
        // 如果有 params 参数，则拼接成带参数的 URL
        const queryString = Object.keys(params)
            .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
            .join('&');
        window.open(url + '?' + queryString, '_blank');
    }
}
export function newPageNeedToken(url, params = {}) {
    // 从本地存储中获取存储的信息
    const token = localStorage.getItem('token');
    if (token) {
        const headers = {
            'Authorization': `Bearer ${token}`
        };
        request.post("http://localhost:9090/account/refresh", {
        }, {
            headers: headers
        })
            .then(res => {
                // 清除本地存储中的 token
                localStorage.removeItem('token');
                localStorage.setItem('token', res.data.token);
                // localStorage.setItem('userId', res.userId);
                localStorage.setItem('userInfo', res.data.userInfo);
                if (res.data.code == 401) {
                    jump('/login');
                    console.log("token不通过");
                    return;
                }
            });
    } else {
        jump('/login');
        console.log("token不通过");
        return;
    }
    console.log("token通过");
    if (Object.keys(params).length === 0) {
        // 如果没有 params 参数，则直接在新窗口打开页面
        window.open(url, '_blank');
    } else {
        // 如果有 params 参数，则拼接成带参数的 URL
        const queryString = Object.keys(params)
            .map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
            .join('&');
        window.open(url + '?' + queryString, '_blank');
    }
}

